package com.szj.pet.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szj.pet.hospital.common.result.Result;
import com.szj.pet.hospital.model.hosp.Notice;
import com.szj.pet.hospital.service.NoticeService;
import com.szj.pet.hospital.vo.hosp.NoticeQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@Api(tags = "公告管理")
@RestController
@RequestMapping("admin/hosp/notice")
//@CrossOrigin
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    //1 查询公告所有信息
    @ApiOperation(value = "获取所有公告信息")
    @GetMapping("findAll")
    public Result findAllNotice() {
        //调用service的方法
        List<Notice> list = noticeService.list();
        return Result.ok(list);
    }

    //2 逻辑删除公告设置
    @ApiOperation(value = "逻辑删除公告信息")
    @DeleteMapping("{id}")
    public Result removeNotice(@PathVariable Long id) {
        boolean flag = noticeService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3 条件查询带分页
    @ApiOperation(value = "公告信息条件查询带分页")
    @PostMapping("findPageNotice/{current}/{limit}")
    public Result findPageNotice(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) NoticeQueryVo noticeQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<Notice> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        String title = noticeQueryVo.getTitle();//标题

        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", noticeQueryVo.getTitle());
        }
        wrapper.orderByDesc("update_time");//倒叙

        //调用方法实现分页查询
        IPage<Notice> pageNotice = noticeService.page(page, wrapper);

        //返回结果
        return Result.ok(pageNotice);
    }

    //4 添加公告信息
    @PostMapping("saveNotice")
    public Result saveNotice(@RequestBody Notice notice) throws ParseException {

        //调用service
        boolean save = noticeService.save(notice);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取公告信息
    @GetMapping("getNotice/{id}")
    public Result getNotice(@PathVariable Long id) {

        Notice notice = noticeService.getById(id);
        return Result.ok(notice);
    }
    //6 修改员工信息
    @PostMapping("updateNotice")
    public Result updateNotice(@RequestBody Notice notice) {
        boolean flag = noticeService.updateById(notice);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除公告信息
    @DeleteMapping("batchRemove")
    public Result batchRemoveNotice(@RequestBody List<Long> idList) {
        noticeService.removeByIds(idList);
        return Result.ok();
    }
}
